package org.example.school.business;

import org.example.school.common.Context;
import org.example.school.common.Result;
import org.example.school.entity.*;
import org.example.school.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CommonInfo {
    @Autowired
    private ITeachersService teachersService;
    @Autowired
    private IStudentsService studentsService;
    @Autowired
    private IParentsService parentsService;
    @Autowired
    private IClassesService classesService;
    @Autowired
    private ITeacherClassRelationsService teacherClassRelationsService;
    @Autowired
    private ISubjectsService subjectsService;
    /**
     * 获取教师角色信息
     */
    protected Result<Teachers> getUserByTeacher() {
        Long userId= Context.getId();
        return Result.success(teachersService.lambdaQuery().eq(Teachers::getUserId, userId).one());
    }

    /**
     * 获取学生角色信息
     */
    protected Result<Students> getUserByStudent() {
        Long userId= Context.getId();
        return Result.success(studentsService.lambdaQuery().eq(Students::getUserId, userId).one());
    }

    /**
     * 获取家长角色信息
     */
    protected Result<Parents> getUserByParent() {
        Long userId= Context.getId();
        return Result.success(parentsService.lambdaQuery().eq(Parents::getUserId, userId).one());
    }

    //获取教师班级课程关系
    protected Result<List<TeacherClassRelations>> getClassInfo() {
        Result<Teachers> userByTeacher = this.getUserByTeacher();
        List<TeacherClassRelations> list = teacherClassRelationsService.lambdaQuery().eq(TeacherClassRelations::getTeacherId, userByTeacher.getData().getId()).list();
       return Result.success(list);
    }
    
    //获取学科
    protected Result<List<Subjects>> getSubjectInfo(List<TeacherClassRelations>  list) {
        List<Subjects> list1 = new ArrayList<>();
        for (TeacherClassRelations teacherClassRelation : list) {
            Subjects subjectsServiceById = subjectsService.getById(teacherClassRelation.getSubjectId());
            list1.add(subjectsServiceById);
        }
        return Result.success(list1);
    }
    //获取任课老师班级
    protected Result<List<Classes>> getClassInfo(List<TeacherClassRelations>  list) {
        List<Classes> objects = new ArrayList<>();
        for (TeacherClassRelations teacherClassRelation : list) {
            Classes classesServiceById = classesService.getById(teacherClassRelation.getClassId());
            objects.add(classesServiceById);
        }
        return Result.success(objects);
    }
}